package com.tanhua.server.controller;

import com.tanhua.server.service.CommentService;
import com.tanhua.server.service.MovementsMqService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/comments")
public class CommentController {

    @Autowired
    private CommentService commentService;

    @Autowired
    private MovementsMqService movementsMqService;

    /**
     * 分页查询评论列表
     *    请求路径 GET
     *    请求参数：
     */
    @GetMapping
    public ResponseEntity queryCommentsList(String movementId,
                                            @RequestParam(defaultValue = "1") Integer page,
                                            @RequestParam(defaultValue = "10")Integer pagesize) {
        return commentService.queryCommentsList(page,pagesize,movementId);
    }

    /**
     * 发布评论
     *  请求连接：POST
     *  请求参数： body  （movementId，comment）
     */
    @PostMapping
    public ResponseEntity saveComments(@RequestBody Map map) {
        String publishId = (String) map.get("movementId");
        String comment = (String) map.get("comment");
        movementsMqService.commentPublishMsg(publishId);
        return commentService.saveComments(publishId,comment);
    }
}
